const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
      default_score: 0,
      score:0,
      orderId:undefined,
      score_text_arr: ['非常差', '差', '一般', '好', '非常好'],
      score_text: "",
      score_img_arr: [],
      upload_pic: [],
      is_upload: false,
      delete_ico: "../../../images/del.png",
      camera_pic: "../../../images/camera.png",
      time:null,
      upload_img:[],
      text:""
    },
  
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      this._default_score(this.data.default_score);
      this.setData({
        orderId: options.orderId
      })
  
    },
  //初始化星的数量
    _default_score: function (tauch_score = 0) {
      var score_img = [];
      var score = 0;
      for (let i = 0; i < 5; i++) {
        if (i < tauch_score) {
          score_img[i] = "../../../images/1.png"
          score = i;
        } else {
          score_img[i] = "../../../images/3.png"
        }
      }
  
      this.setData({
        score_img_arr: score_img,
        score_text: this.data.score_text_arr[score]
      });
    },
  
    onScore: function (e) {
      var score = e.currentTarget.dataset.score;
      this._default_score(score);
      this.setData({
        score:score
      })
    },
    //选择图片
    chooseImage(e) {
      var that = this;
      wx.chooseImage({
        count: 4,
        sizeType: ['original', 'compressed'],  //可选择原图或压缩后的图片
        sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
        success: res => {
          var addImg = res.tempFilePaths;
          console.log(addImg);
          var upload_pic = that.data.upload_pic;
          for(var i in addImg){
            //判断已选图片数量
            if (upload_pic.length<4){
                upload_pic.push(addImg[i]);
            }
          } 
          var is_upload = false;
          if (upload_pic.length >= 4) {
            is_upload = true;
          }
          that.setData({
            upload_pic: upload_pic,
            is_upload: is_upload
          })
        }
      })
  
    },
  
    onToast: function(e){
      this.submit();
      wx.showToast({
        title: '提交成功',
        icon: 'success',
        duration: 2000
       })
    },
    //上传评论
    submit: function (e) {
      // wx.showLoading({
      //   title: '上传中',
      // })
      var that = this;
      //var is = false;
      var picture_list = that.data.upload_pic;
      var upload_img = [];
      var skushowpricekey = '[';
      //var flag = false;
      for (var i in picture_list) {
        if(i == picture_list.length-1){
        // this.upload_file_server(picture_list[i]).then(res=>{
        //   upload_img.push({url:res});
        //   that.setData({
        //     upload_img: upload_img
        //   })
        // }); 
        var path = picture_list[i];
        var token = app.getToken();
        wx.uploadFile({
          url: 'http://localhost:8080' + '/community/service/rules/uploadFile1',
          filePath: path,
          name: 'file',
          header:{
            'content-type': 'application/json',
            'Authorization': token
          },
          success: res => {
            console.log(res.data);
            skushowpricekey+= '"';
            skushowpricekey+= res.data;
            skushowpricekey+= '"]';
	          console.log(222222222222222222222,skushowpricekey)  
            console.log(this.data.upload_img)
            let data1 = {
              content: this.data.text,
              goodsId: this.data.orderId,
              contentPicture: skushowpricekey,
              score: this.data.score,
            }
          app.sendPostRequest('/community/ec/comment', data1,(res) => {
          if(res.code == 200) {
          // that.setData({
          //   [s]: false
          // })
          wx.showToast({
              title: '评论成功！',  
              icon: 'success',  
              duration: 1500
          })
          this.setData({
            text: " ",
            score: 0,
            upload_pic:[]

          });
        } else {
          wx.showToast({
              title: '评论失败！',  
              icon: 'error',  
              duration: 1500
          })
        }

      })
          }
        });
      }
      else{
        var path = picture_list[i];
        var token = app.getToken();
        wx.uploadFile({
          url: 'http://localhost:8080' + '/community/service/rules/uploadFile1',
          filePath: path,
          name: 'file',
          header:{
            'content-type': 'application/json',
            'Authorization': token
          },
          success: res => {
            console.log(res.data);

            skushowpricekey+='"'+res.data +'",';
            
            console.log(11111111111111111,skushowpricekey)
            //upload_img.push(res.data)
            //resolve(res.data);
            // wx.showToast({
            //   title: '提交成功',
            //   icon: 'success',
            //   duration: 2000
            //  })
          }
        });
      }
    }
      this.setData({
        text: e.detail.value.text,
        upload_img: upload_img,
      })
     
      
          
    },


    //选择并上传图片--Max：9张
  selectImage: function () {
    wx.chooseImage({
      count: 4,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
 
      success: function (res) {
        //将图片路径循环赋值给filePath参数
        for (var i = 0; i < res.tempFilePaths.length; i++) {
 
          var imgUrl = res.tempFilePaths[i];
 
          var token = app.getToken();
          wx.uploadFile({
            //上传图片的网路请求地址
            url: 'http://localhost:8080/community/service/rules/uploadFile1',
            //选择
            filePath: imgUrl,
            name: 'file',
            header:{
              'content-type': 'application/json',
              'Authorization': token
            },
 
            success: function (res) {
              //console.log(res);
              //var url = JSON.parse(res.data);
            },
 
            fail: function (res) {
              console.log("error");
            }
          });
 
        }//for循环结束
 
      }
    });
 
    console.log("****正在添加图片*****");
 
  },
   setTime:function(){
     var that= this;
     that.data.token = app.getToken()
     console.log(that.data.token)
     var time = setTimeout(function(){
       var upload_img = that.data.upload_img;
       var picture_list = that.data.upload_pic;
       //判断是否成功
       if (upload_img.length == picture_list.length){
         //清除定时
         clearTimeout(that.data.time);
         //添加评论
        //  wx.request({
        //    url: 'http://localhost:8080' + '/comment/add',
        //    method: "POST",
        //    header:{
        //     'content-type': 'application/json',
        //     'Authorization':  that.data.token
        //   },
        //    data: {
        //      text: that.data.text,
        //      urls: JSON.stringify(upload_img),
        //      score1: that.data.score
        //    },
        //    success:function(res){
        //      wx.hideLoading();
        //      console.log(res);
        //    }
        //  });
        app.sendPostRequest("/comment/add", {
          text: that.data.text,
          urls: JSON.stringify(upload_img),
          score1: that.data.score
        },(res) => {
          wx.hideLoading();
             console.log(res);
        })
       }else{
         //执行定时判断
         that.setTime();
       }
     },400);
     that.setData({
       time:time
     })
   },
  //上传图片
    upload_file_server: function (path) {
      var that= this;
      that.data.token = app.getToken()
      console.log(that.data.token)
      // return new Promise(function(resolve, reject){
      wx.uploadFile({
        url: 'http://localhost:8080' + '/community/service/rules/uploadFile1',
        filePath: path,
        name: 'file',
        header:{
          'content-type': 'application/json',
          'Authorization': that.data.token
        },
        success: res => {
          console.log(res);
          //resolve(res.data);
        }
      });
      //});
    },
  //删除图片
    deletePic: function (e) {
      var pic_index = e.currentTarget.dataset.pic_index;
      var upload_pic = [];
      for (let i in this.data.upload_pic) {
        if (i != pic_index) {
          upload_pic.push(this.data.upload_pic[i])
        }
      }
      this.setData({
        upload_pic: upload_pic,
        is_upload: false
      })
  
    },
  
  
   
  
  })